package com.example.service.impl.ums;

import com.example.common.DozerUtils;
import com.example.common.UUIDUtils;
import com.example.mapper.ums.RoleMenuMapper;
import com.example.mapper.ums.UmsRoleMapper;
import com.example.model.ums.RoleMenu;
import com.example.model.ums.UmsRole;
import com.example.model.vo.UmsRoleVo;
import com.example.service.ums.UmsRoleService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * 角色Service
 */
@Service
@Transactional
public class UmsRoleServiceImpl implements UmsRoleService {

    @Autowired
    private UmsRoleMapper mapper;

    @Autowired
    private RoleMenuMapper roleMenuMapper;

    @Override
    public PageInfo<UmsRole> findUmsRolePageByParam(UmsRoleVo umsRole) {
        PageHelper.startPage(umsRole.getPage(),umsRole.getRows());//分页起始码以及每页页数
        UmsRole conversion = DozerUtils.conversion(umsRole, UmsRole.class);
        List<UmsRole> umsRoleByParam = mapper.findUmsRolesByParam(conversion);
        PageInfo pageInfo=new PageInfo(umsRoleByParam);
        return pageInfo;
    }

    @Override
    public List<UmsRole> findUmsRoleByParam(UmsRole umsRole) {
        List<UmsRole> umsRoleByParam = mapper.findUmsRolesByParam(umsRole);
        return umsRoleByParam;
    }

    @Override
    public int insert(UmsRole role) {
        role.setId(UUIDUtils.getId());
        role.setCreateTime(new Date());
        return mapper.insert(role);
    }

    @Override
    public void deleteById(String id) {
        //查询关联的菜单
        RoleMenu roleMenu = new RoleMenu();
        roleMenu.setRoleId(id);
        List<RoleMenu> roleMenuByParam = roleMenuMapper.findRoleMenuByParam(roleMenu);
        if(roleMenuByParam.size() != 0){
            roleMenuMapper.deleteByRoleId(id);
        }
        mapper.deleteById(id);
    }
}
